package com_djm_2025_06_05.demo1;

public class Test {

    public static void main(String[] args) {
        Animal [] animals = new Animal[2];

        animals[0] = new Bird("云中君",22);
        animals[1] = new Dog("斯派克",5);

        //调用父类和子类都定义的方法，体现动态绑定
        for (Animal animal : animals) {
            animal.eat();
        }

        //一条华丽的分割线
        System.out.println("---------------------------------------");

        //利用instanceof运算符进行模式匹配，然后调用子类定义的方法

        for (Animal animal : animals) {
            if(animal instanceof Bird) {
                Bird bird = (Bird) animal;
                bird.fly();
            }

            if(animal instanceof Dog) {
                Dog dog = (Dog) animal;
                dog.bark();
            }
        }
    }
}
